﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_Sim : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GlobalFunctions gf = new GlobalFunctions();
        if (gf.IsLogin() != 1) {
            Response.Redirect("~/admin");
        }
        if(!IsPostBack){
            string sid = Request.QueryString["edit"];
            if (sid != null && sid != "")
            {
                btnThemSim.Text = "Cập nhật";
                btnCancel.Visible = true;

                SimDSTableAdapters.Sim2TableAdapter ad = new SimDSTableAdapters.Sim2TableAdapter();
                SimDS.Sim2DataTable table = ad.GetDataBySID(int.Parse(sid));

                txtSoSim.Text = table[0].SoSim;
                txtGia.Text = table[0].Gia.ToString();               

                ddLoai.DataBound += new EventHandler(DDLoai_DataBound);
                ddMang.DataBound += new EventHandler(DDMang_DataBound);               
            }
        }

        
    }
    protected void DDLoai_DataBound(object sender, EventArgs e) {
        string sid = Request.QueryString["edit"];
        SimDSTableAdapters.Sim2TableAdapter ad = new SimDSTableAdapters.Sim2TableAdapter();
        SimDS.Sim2DataTable table = ad.GetDataBySID(int.Parse(sid));
        ddLoai.Items.FindByValue(table[0].TID.ToString()).Selected = true;
    }
    protected void DDMang_DataBound(object sender, EventArgs e) {
        string sid = Request.QueryString["edit"];
        SimDSTableAdapters.Sim2TableAdapter ad = new SimDSTableAdapters.Sim2TableAdapter();
        SimDS.Sim2DataTable table = ad.GetDataBySID(int.Parse(sid));
        ddMang.Items.FindByValue(table[0].MID.ToString()).Selected = true;
    }

    protected void txtThemNhaMang_Click(object sender, EventArgs e) {
        if (txtNhaMang.Text == "" || txtLogo.Text == ""){
            Response.Write("<script>alert('Tên nhà mạng hoặc logo không được rỗng!')</script>");
            return;
        }

        SimDSTableAdapters.tNhaMangTableAdapter ad = new SimDSTableAdapters.tNhaMangTableAdapter();
        ad.Insert(txtNhaMang.Text, txtLogo.Text);
        Response.Redirect("~/admin/Sim.aspx");
    }
    protected void btnThemLoai_Click(object sender, EventArgs e) {
        if (txtLoai.Text == ""){
            Response.Write("<script>alert('Tên loại không được rỗng!')</script>");
            return;
        }

        SimDSTableAdapters.tLoaiSimTableAdapter ad = new SimDSTableAdapters.tLoaiSimTableAdapter();
        ad.Insert(txtLoai.Text);

        Response.Redirect("~/admin/Sim.aspx");
    }
    protected void btnThemSim_Click(object sender, EventArgs e) {
        if (btnThemSim.Text == "Thêm") {
            if (txtSoSim.Text == "") {
                Response.Write("<script>alert('Số sim không được rỗng!')</script>");
                return;
            }
            decimal gia = 0;
            try {
                gia = Decimal.Parse(txtGia.Text);
            }
            catch (System.Exception ex) {
                Response.Write("<script>alert('Giá không hợp lệ!')</script>");
                return;
            }

            SimDSTableAdapters.tSimTableAdapter ad = new SimDSTableAdapters.tSimTableAdapter();
            ad.Insert(txtSoSim.Text, gia, int.Parse(ddMang.SelectedValue), int.Parse(ddLoai.SelectedValue));

            Response.Redirect("~/admin/Sim.aspx");
        }
        else { // cap nhat

            if (txtSoSim.Text == "") {
                Response.Write("<script>alert('Số sim không được rỗng!')</script>");
                return;
            }
            decimal gia = 0;
            try {
                gia = Decimal.Parse(txtGia.Text);
            }
            catch (System.Exception ex) {
                Response.Write("<script>alert('Giá không hợp lệ!')</script>");
                return;
            }

            string sid = Request.QueryString["edit"];
            SimDSTableAdapters.tSimTableAdapter ad = new SimDSTableAdapters.tSimTableAdapter();
            ad.Update(txtSoSim.Text, decimal.Parse(txtGia.Text), int.Parse(ddMang.SelectedValue), int.Parse(ddLoai.SelectedValue), int.Parse(sid));
            Response.Redirect("~/admin/Sim.aspx");
            

        }
           
        
    }
    protected void btnCancel_Click(object sender, EventArgs e) {
        btnThemSim.Text = "Thêm";
        btnCancel.Visible = false;
        txtSoSim.Text = "";
        txtGia.Text = "";
        ddMang.SelectedIndex = 0;
        ddLoai.SelectedIndex = 0;

    }
}
